<?php $this->context->layout = 'form'; ?>
<?= \common\widgets\Asset::widget(['type' => ['summernote', 'tagsinput','select2','dragula']]) ?>
<style>
    .form-label-asterisk {
        color: red;
    }
    .freightset .iradio-blue{
        margin-top: 6px !important;
    }
</style>
<form class="form-horizontal m" id="form-basics-data">
    <div class="layui-tab" lay-filter="test1">
        <ul class="layui-tab-title">
            <?php foreach ($addAll as $key => $para): ?>
                <li class="<?= $key == 0 ? 'layui-this' : '' ?>" data-formname="<?= $para['form'] ?>"
                    lay-id="<?= $key ?>">
                    <?= $para['name'] ?>
                </li>
            <?php endforeach; ?>
        </ul>
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-show" data-id="0">
                <input type="hidden" name="id" value="<?=$info['id']?>">
                <?= \common\widgets\FormInput::widget(['name' => 'forminput|商品名称', 'extend' => ['name' => 'name', 'required' => 1,'info'=>$info]]) ?>
                <?= \common\widgets\FormInput::widget(['name'=>'formselect|分类','extend'=>['name'=>'classify_id','required'=>1,'data'=>$classify??[],'showvalue'=>'id','showname'=>'name','place'=>'','class'=>'select2','info'=>$info]])?>
                <?= \common\widgets\FormInput::widget(['name' => 'forminput|价格', 'extend' => ['name' => 'price', 'required' => 1,'type'=>'number','info'=>$info]]) ?>
                <?= \common\widgets\FormInput::widget(['name' => 'image|商品主图', 'extend' => ['data'=>$info['main_img'],'link'=>'false','name' => 'imgarr1', 'id' => 'uploadimgbtn1', 'script_after' => '1', 'cat' => 'bmimage', 'water' => 'true', 'required' => 1, 'sm' => 3, 'tips' => '建议尺寸：宽165px*高160px，jpg，jpeg，png格式']]) ?>
                <input type="hidden" name="main_img" id="img1" value="">
                <?= \common\widgets\FormInput::widget(['name' => 'image|图片', 'extend' => ['data'=>$info['img'],'link'=>'false','name' => 'imgarr2', 'id' => 'uploadimgbtn2', 'multi' => 'true', 'drag' => 'true', 'cat' => 'bmimage', 'water' => 'true', 'required' => 1, 'sm' => 3, 'script_after' => '2', 'tips' => '可拖动排序，建议尺寸：宽375px*高275px，jpg，jpeg，png格式']]) ?>
                <input type="hidden" name="img" id="img2" value="">
                <?= \common\widgets\FormInput::widget(['name'=>'forminput|特色','extend'=>['name'=>'feature','id' => 'feature','class'=>'tags','required'=>1,'info'=>$info]])?>
                <?= \common\widgets\FormInput::widget(['name' => 'forminput|销售量', 'extend' => ['name' => 'sales_volume','type'=>'number', 'required' => 1,'info'=>$info]]) ?>
                <?= \common\widgets\FormInput::widget(['name' => 'forminput|排序', 'extend' => ['name' => 'sort','type'=>'number', 'required' => 1,'info'=>$info,'值越大越靠前']]) ?>
                <?= \common\widgets\FormInput::widget(['name'=>'formradio|状态','extend'=>['name'=>'status','required'=>1,'info'=>$info]])?>
            </div>
            <div class="layui-tab-item" data-id="1">
                <table id="more-spec-lists-table-1" class="layui-table spec-lists-table">
                    <thead>
                    <tr style="background-color: #f3f5f9">
                        <th><span class="form-label-asterisk">*</span>规格名称</th>
                        <th><span class="form-label-asterisk">*</span>价格(元)</th>
                        <th><span class="form-label-asterisk">*</span>库存</th>
                        <th><span class="form-label-asterisk">*</span>销售量</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($specAll as $key => $value): ?>
                        <tr>
                            <td>
                                <input type="text" autocomplete="off" class="form-control" placeholder="请输入名称"
                                       name="specAll[<?=$key?>][name]" value="<?=$value['name']?>" required>
                            </td>
                            <td>
                                <input type="number" autocomplete="off" class="form-control" placeholder="请输入价格"
                                       name="specAll[<?=$key?>][price]" value="<?=$value['price']?>" required>
                            </td>
                            <td>
                                <input type="number" autocomplete="off" class="form-control" placeholder="请输入库存"
                                       name="specAll[<?=$key?>][stock]" value="<?=$value['stock']?>" required>
                            </td>
                            <td>
                                <input type="number" autocomplete="off" class="form-control" placeholder="请输入销售量"
                                       name="specAll[<?=$key?>][sales_volume]" value="<?=$value['sales_volume']?>" required>
                            </td>
                            <td>
                                <a class="btn btn-success btn-xs add"><i class="fa fa-plus"></i> 新增</a>
                                <a class="btn btn-danger btn-xs delete"><i class="fa fa-trash"></i> 删除</a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <div class="layui-tab-item" data-id="2">
                <div class="form-group">
                    <label class="col-sm-2 control-label">商品详情：</label>
                    <div class="col-sm-9">
                        <?= \common\widgets\FormInput::widget(['name' => 'textarea', 'extend' => ['height' => '500', 'name' => 'details', 'class' => 'summernote_content hide','info'=>$info]]) ?>
                        <div class="summernote" data-place="" data-height="600px"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
<script type="text/html" id="template-spec-1">
    <tr>
        <td>
            <input type="text"  autocomplete="off" class="form-control" placeholder="请输入名称" name="specAll[{i}][name]" value="{name}" required>
        </td>
        <td>
            <input type="number"  autocomplete="off" class="form-control" placeholder="请输入价格" name="specAll[{i}][price]" value="{price}" required>
        </td>
        <td>
            <input type="number"  autocomplete="off" class="form-control" placeholder="请输入价格" name="specAll[{i}][stock]" value="{stock}" required>
        </td>
        <td>
            <input type="number"  autocomplete="off" class="form-control" placeholder="请输入销售量" name="specAll[{i}][sales_volume]" value="{sales_volume}" required>
        </td>
        <td>
            <a class="btn btn-success btn-xs add"><i class="fa fa-plus"></i> 新增</a>
            <a class="btn btn-danger btn-xs delete"><i class="fa fa-trash"></i> 删除</a>
        </td>
    </tr>
</script>
<?php $this->beginBlock('script'); ?>
<script>
    var elementtt;
    var formName = '<?=$addAll[0]['form']?>';
    settagsInput('#feature','添加标签',4)
    function submitHandler() {
        if (true) {
            imgassign("input[name='imgarr1[]']",'#img1')
            imgassign("input[name='imgarr2[]']",'#img2')
            var sHTML = $('.summernote').summernote('code');
            $(".summernote_content").val(sHTML);
            $.operate.save(oesUrl, $('#form-basics-data').serialize(),errorJump);
        }
    }
    function errorJump(result) {
        if (result.code==-1){
            elementtt.tabChange('test1', result.data.id); //根据传入的id传入到指定的tab项
        }
    }
    var is_error = 0;
    function errorPlacement(error, element) {
        var id = $(element.parent()).closest('.layui-tab-item').attr('data-id')
        elementtt.tabChange('test1', id); //根据传入的id传入到指定的tab项
        error.appendTo(element.parent());
    }

    layui.use('element', function () {
        elementtt = layui.element;
        elementtt.on('tab', function (elem) {
            formName = $(this).attr('data-formname')
        });
    });
    /**
     * 规格触发
     */
    var currentType = 1;
    function spectemplate(data,i) {
        var html = $('#template-spec-1').html();
        var oReg =new RegExp(`{i}`,"g");
        html = html.replace(oReg, i)
        for (var j in data){
            var oReg =new RegExp(`{${j}}`,"g");
            html = html.replace(oReg, data[j])
        }
        return html;
    }
    $('body').on('click','.add',function () {
        var i = $(this).closest('tbody').find('tr').size();
        var html = spectemplate({name:'',stock:'',price:'',sales_volume:0},i)
        $(this).closest('tbody').append(html)
    })
    $('body').on('click','.delete',function () {
        var i = $(this).closest('tbody').find('tr').size();
        if (i<=1){
            $.modal.msgError('第一个不允许删除');
            return
        }
        $(this).closest('tr').remove();
    })
</script>
<?php $this->endBlock(); ?>
